package com.tadpole.music.view.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.presenter.me.UpdateUserInfoPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.activity.login.LoginActivity;

/**
 * 修改昵称
 * Created By 孙岩
 * on 2020/9/27
 */
public class NickNameActivity extends BaseActivity implements RegisterIView {
    //回退
    private View view_back_icon;
    //标题
    private TextView actionbar_title;
    //昵称
    private EditText etNickName;
    //确定
    private TextView tvSubmit;

    private UpdateUserInfoPresenter updateUserInfoPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_name);
        initViews();
        initListeners();

        updateUserInfoPresenter = new UpdateUserInfoPresenter();
        updateUserInfoPresenter.attachView(this);
    }


    //初始化控件
    private void initViews() {
        view_back_icon = findViewById(R.id.view_back_icon);
        actionbar_title = findViewById(R.id.actionbar_title);
        actionbar_title.setText("修改昵称");
        tvSubmit = findViewById(R.id.tvSubmit);
        etNickName = findViewById(R.id.etNickName);
    }

    //初始化监听器
    private void initListeners(){
        view_back_icon.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                NickNameActivity.this.finish();
            }
        });

        tvSubmit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(etNickName.getText().toString().trim().equals("")){
                       ToastUtils.show("请输入昵称");
                    }else{
                        updateUserInfoPresenter.updateUserInfo(1,etNickName.getText().toString().trim());
                    }
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

    }

    @Override
    public void showResult() {
        ToastUtils.show("修改昵称成功");
        setResult(100);
        this.finish();
    }

    @Override
    public void show401() {
        SpUtil.getInstance(this).putString(Constant.TOKEN,"");
        Intent intent = new Intent(NickNameActivity.this, LoginActivity.class);
        startActivity(intent);
    }
}
